home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 242 / Issue 242 - April 2008 - DPCS0408DVD.ISO / Open Source / AutoHotKey / Source / AutoHotkey104705_source.exe / source / globaldata.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-06-12  |  13.5 KB  |  323 lines

  1. /*
  2. AutoHotkey
  3.  
  4. Copyright 2003-2007 Chris Mallett (support@autohotkey.com)
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15. */
  16.  
  17. #ifndef globaldata_h
  18. #define globaldata_h
  19.  
  20. #include "hook.h" // For KeyHistoryItem and probably other things.
  21. #include "clipboard.h"  // For the global clipboard object
  22. #include "script.h" // For the global script object and g_ErrorLevel
  23. #include "os_version.h" // For the global OS_Version object
  24.  
  25. extern HINSTANCE g_hInstance;
  26. extern DWORD g_MainThreadID;
  27. extern DWORD g_HookThreadID;
  28. extern CRITICAL_SECTION g_CriticalRegExCache;
  29.  
  30. extern bool g_DestroyWindowCalled;
  31. extern HWND g_hWnd;  // The main window
  32. extern HWND g_hWndEdit;  // The edit window, child of main.
  33. extern HWND g_hWndSplash;  // The SplashText window.
  34. extern HFONT g_hFontSplash;
  35. extern HACCEL g_hAccelTable; // Accelerator table for main menu shortcut keys.
  36.  
  37. typedef int (WINAPI *StrCmpLogicalW_type)(LPCWSTR, LPCWSTR);
  38. extern StrCmpLogicalW_type g_StrCmpLogicalW;
  39. extern WNDPROC g_TabClassProc;
  40.  
  41. extern modLR_type g_modifiersLR_logical;   // Tracked by hook (if hook is active).
  42. extern modLR_type g_modifiersLR_logical_non_ignored;
  43. extern modLR_type g_modifiersLR_physical;  // Same as above except it's which modifiers are PHYSICALLY down.
  44.  
  45. #ifdef FUTURE_USE_MOUSE_BUTTONS_LOGICAL
  46. extern WORD g_mouse_buttons_logical; // A bitwise combination of MK_LBUTTON, etc.
  47. #endif
  48.  
  49. #define STATE_DOWN 0x80
  50. #define STATE_ON 0x01
  51. extern BYTE g_PhysicalKeyState[VK_ARRAY_COUNT];
  52. extern bool g_BlockWinKeys;
  53. extern DWORD g_HookReceiptOfLControlMeansAltGr;
  54. extern DWORD g_IgnoreNextLControlDown;
  55. extern DWORD g_IgnoreNextLControlUp;
  56.  
  57. // If a SendKeys() operation takes longer than this, hotkey's modifiers won't be pressed back down:
  58. extern int g_HotkeyModifierTimeout;
  59. extern int g_ClipboardTimeout;
  60.  
  61. extern HHOOK g_KeybdHook;
  62. extern HHOOK g_MouseHook;
  63. extern HHOOK g_PlaybackHook;
  64. extern bool g_ForceLaunch;
  65. extern bool g_WinActivateForce;
  66. extern SingleInstanceType g_AllowOnlyOneInstance;
  67. extern bool g_persistent;
  68. extern bool g_NoEnv;
  69. extern bool g_NoTrayIcon;
  70. #ifdef AUTOHOTKEYSC
  71.     extern bool g_AllowMainWindow;
  72. #endif
  73. extern bool g_AllowSameLineComments;
  74. extern bool g_AllowInterruption;
  75. extern bool g_DeferMessagesForUnderlyingPump;
  76. extern bool g_MainTimerExists;
  77. extern bool g_UninterruptibleTimerExists;
  78. extern bool g_AutoExecTimerExists;
  79. extern bool g_InputTimerExists;
  80. extern bool g_DerefTimerExists;
  81. extern bool g_SoundWasPlayed;
  82. extern bool g_IsSuspended;
  83. extern int g_nLayersNeedingTimer;
  84. extern int g_nThreads;
  85. extern int g_nPausedThreads;
  86. extern bool g_IdleIsPaused;
  87. extern int g_MaxHistoryKeys;
  88.  
  89. extern VarSizeType g_MaxVarCapacity;
  90. // This value is the absolute limit:
  91. #define MAX_THREADS_LIMIT 20
  92. #define MAX_THREADS_DEFAULT 10
  93. extern UCHAR g_MaxThreadsPerHotkey;
  94. extern int g_MaxThreadsTotal;
  95. extern int g_MaxHotkeysPerInterval;
  96. extern int g_HotkeyThrottleInterval;
  97. extern bool g_MaxThreadsBuffer;
  98. extern HotCriterionType g_HotCriterion;
  99. extern char *g_HotWinTitle;
  100. extern char *g_HotWinText;
  101. extern HotkeyCriterion *g_FirstHotCriterion, *g_LastHotCriterion;
  102.  
  103. extern MenuTypeType g_MenuIsVisible;
  104. extern int g_nMessageBoxes;
  105. extern int g_nInputBoxes;
  106. extern int g_nFileDialogs;
  107. extern int g_nFolderDialogs;
  108. extern InputBoxType g_InputBox[MAX_INPUTBOXES];
  109. extern SplashType g_Progress[MAX_PROGRESS_WINDOWS];
  110. extern SplashType g_SplashImage[MAX_SPLASHIMAGE_WINDOWS];
  111. EXTERN_GUI;
  112. extern HWND g_hWndToolTip[MAX_TOOLTIPS];
  113. extern MsgMonitorStruct *g_MsgMonitor; // An array to be allocated upon first use (if any).
  114. extern int g_MsgMonitorCount;
  115.  
  116. extern UCHAR g_SortCaseSensitive;
  117. extern bool g_SortNumeric;
  118. extern bool g_SortReverse;
  119. extern int g_SortColumnOffset;
  120. extern Func *g_SortFunc;
  121.  
  122. extern char g_delimiter;
  123. extern char g_DerefChar;
  124. extern char g_EscapeChar;
  125.  
  126. // Hot-string vars:
  127. extern char g_HSBuf[HS_BUF_SIZE];
  128. extern int g_HSBufLength;
  129. extern HWND g_HShwnd;
  130.  
  131. // Hot-string global settings:
  132. extern int g_HSPriority;
  133. extern int g_HSKeyDelay;
  134. extern SendModes g_HSSendMode;
  135. extern bool g_HSCaseSensitive;
  136. extern bool g_HSConformToCase;
  137. extern bool g_HSDoBackspace;
  138. extern bool g_HSOmitEndChar;
  139. extern bool g_HSSendRaw;
  140. extern bool g_HSEndCharRequired;
  141. extern bool g_HSDetectWhenInsideWord;
  142. extern bool g_HSDoReset;
  143. extern bool g_HSResetUponMouseClick;
  144. extern char g_EndChars[HS_MAX_END_CHARS + 1];
  145.  
  146. // Global objects:
  147. extern Var *g_ErrorLevel;
  148. extern input_type g_input;
  149. EXTERN_SCRIPT;
  150. EXTERN_CLIPBOARD;
  151. EXTERN_OSVER;
  152.  
  153. extern int g_IconTray;
  154. extern int g_IconTraySuspend;
  155.  
  156. extern DWORD g_OriginalTimeout;
  157.  
  158. EXTERN_G;
  159. extern global_struct g_default;
  160.  
  161. extern char g_WorkingDir[MAX_PATH];  // Explicit size needed here in .h file for use with sizeof().
  162. extern char *g_WorkingDirOrig;
  163.  
  164. extern bool g_ContinuationLTrim;
  165. extern bool g_ForceKeybdHook;
  166. extern ToggleValueType g_ForceNumLock;
  167. extern ToggleValueType g_ForceCapsLock;
  168. extern ToggleValueType g_ForceScrollLock;
  169.  
  170. extern ToggleValueType g_BlockInputMode;
  171. extern bool g_BlockInput;  // Whether input blocking is currently enabled.
  172. extern bool g_BlockMouseMove; // Whether physical mouse movement is currently blocked via the mouse hook.
  173.  
  174. extern Action g_act[];
  175. extern int g_ActionCount;
  176. extern Action g_old_act[];
  177. extern int g_OldActionCount;
  178.  
  179. extern key_to_vk_type g_key_to_vk[];
  180. extern key_to_sc_type g_key_to_sc[];
  181. extern int g_key_to_vk_count;
  182. extern int g_key_to_sc_count;
  183.  
  184. extern KeyHistoryItem *g_KeyHistory;
  185. extern int g_KeyHistoryNext;
  186. extern DWORD g_HistoryTickNow;
  187. extern DWORD g_HistoryTickPrev;
  188. extern HWND g_HistoryHwndPrev;
  189. extern DWORD g_TimeLastInputPhysical;
  190.  
  191. #ifdef ENABLE_KEY_HISTORY_FILE
  192. extern bool g_KeyHistoryToFile;
  193. #endif
  194.  
  195.  
  196. // 9 might be better than 10 because if the granularity/timer is a little
  197. // off on certain systems, a Sleep(10) might really result in a Sleep(20),
  198. // whereas a Sleep(9) is almost certainly a Sleep(10) on OS's such as
  199. // NT/2k/XP.  UPDATE: Roundoff issues with scripts having
  200. // even multiples of 10 in them, such as "Sleep,300", shouldn't be hurt
  201. // by this because they use GetTickCount() to verify how long the
  202. // sleep duration actually was.  UPDATE again: Decided to go back to 10
  203. // because I'm pretty confident that that always sleeps 10 on NT/2k/XP
  204. // unless the system is under load, in which case any Sleep between 0
  205. // and 20 inclusive seems to sleep for exactly(?) one timeslice.
  206. // A timeslice appears to be 20ms in duration.  Anyway, using 10
  207. // allows "SetKeyDelay, 10" to be really 10 rather than getting
  208. // rounded up to 20 due to doing first a Sleep(10) and then a Sleep(1).
  209. // For now, I'm avoiding using timeBeginPeriod to improve the resolution
  210. // of Sleep() because of possible incompatibilities on some systems,
  211. // and also because it may degrade overall system performance.
  212. // UPDATE: Will get rounded up to 10 anyway by SetTimer().  However,
  213. // future OSs might support timer intervals of less than 10.
  214. #define SLEEP_INTERVAL 10
  215. #define SLEEP_INTERVAL_HALF (int)(SLEEP_INTERVAL / 2)
  216.  
  217. // The first timers in the series are used by the MessageBoxes.  Start at +2 to give
  218. // an extra margin of safety:
  219. enum OurTimers {TIMER_ID_MAIN = MAX_MSGBOXES + 2, TIMER_ID_UNINTERRUPTIBLE, TIMER_ID_AUTOEXEC
  220.     , TIMER_ID_INPUT, TIMER_ID_DEREF};
  221.  
  222. // MUST MAKE main timer and uninterruptible timers associated with our main window so that
  223. // MainWindowProc() will be able to process them when it is called by the DispatchMessage()
  224. // of a non-standard message pump such as MessageBox().  In other words, don't let the fact
  225. // that the script is displaying a dialog interfere with the timely receipt and processing
  226. // of the WM_TIMER messages, including those "hidden messages" which cause DefWindowProc()
  227. // (I think) to call the TimerProc() of timers that use that method.
  228. // Realistically, SetTimer() called this way should never fail?  But the event loop can't
  229. // function properly without it, at least when there are suspended subroutines.
  230. // MSDN docs for SetTimer(): "Windows 2000/XP: If uElapse is less than 10,
  231. // the timeout is set to 10."
  232. #define SET_MAIN_TIMER \
  233. if (!g_MainTimerExists)\
  234.     g_MainTimerExists = SetTimer(g_hWnd, TIMER_ID_MAIN, SLEEP_INTERVAL, (TIMERPROC)NULL);
  235. // v1.0.39 for above: Apparently, one of the few times SetTimer fails is after the thread has done
  236. // PostQuitMessage. That particular failure was causing an unwanted recursive call to ExitApp(),
  237. // which is why the above no longer calls ExitApp on failure.  Here's the sequence:
  238. // Someone called ExitApp (such as the max-hotkeys-per-interval warning dialog).
  239. // ExitApp() removes the hooks.
  240. // The hook-removal function calls MsgSleep() while waiting for the hook-thread to finish.
  241. // MsgSleep attempts to set the main timer so that it can judge how long to wait.
  242. // The timer fails and calls ExitApp even though a previous call to ExitApp is currently underway.
  243.  
  244. // When someone calls SET_UNINTERRUPTIBLE_TIMER, by definition the current script subroutine is
  245. // becoming non-interruptible.  Therefore, their should never be a need to have more than one
  246. // of these timer going simultanously since they're only created for the launch of a new
  247. // quasi-thread, which is forbidden when the current thread is uninterruptible.
  248. // Remember than the 2nd param of SetTimer() is ignored when the 1st param is NULL.
  249. // For this one, the timer is not recreated if it already exists because I think SetTimer(), when
  250. // called with NULL as a first parameter, may wind up creating more than one Timer and we only
  251. // want one of these to exist at a time.
  252. // The caller should ensure that aTimeoutValue is <= INT_MAX because otherwise SetTimer()'s behavior
  253. // will vary depending on OS type & version.
  254. // Also have this one abort on unexpected error, since failure to set the timer might result in the
  255. // script becoming permanently uninterruptible (which prevents new hotkeys from being activated
  256. // even though the program is still responsive).
  257. #define SET_UNINTERRUPTIBLE_TIMER \
  258. if (!g_UninterruptibleTimerExists)\
  259.     g_UninterruptibleTimerExists = SetTimer(g_hWnd, TIMER_ID_UNINTERRUPTIBLE \
  260.         , g_script.mUninterruptibleTime < 10 ? 10 : g_script.mUninterruptibleTime, UninterruptibleTimeout);
  261. // v1.0.39 for above: Removed the call to ExitApp() upon failure.  See SET_MAIN_TIMER for details.
  262.  
  263. #define KILL_UNINTERRUPTIBLE_TIMER \
  264. if (g_UninterruptibleTimerExists && KillTimer(g_hWnd, TIMER_ID_UNINTERRUPTIBLE))\
  265.     g_UninterruptibleTimerExists = false;
  266.  
  267. // See AutoExecSectionTimeout() for why g.AllowThreadToBeInterrupted is used rather than the other var.
  268. // Also, from MSDN: "When you specify a TimerProc callback function, the default window procedure calls the
  269. // callback function when it processes WM_TIMER. Therefore, you need to dispatch messages in the calling thread,
  270. // even when you use TimerProc instead of processing WM_TIMER."  My: This is why all TimerProc type timers
  271. // should probably have a window rather than passing NULL as first param of SetTimer().
  272. #define SET_AUTOEXEC_TIMER(aTimeoutValue) \
  273. {\
  274.     g.AllowThreadToBeInterrupted = false;\
  275.     if (!g_AutoExecTimerExists)\
  276.         g_AutoExecTimerExists = SetTimer(g_hWnd, TIMER_ID_AUTOEXEC, aTimeoutValue, AutoExecSectionTimeout);\
  277. } // v1.0.39 for above: Removed the call to ExitApp() upon failure.  See SET_MAIN_TIMER for details.
  278.  
  279. #define SET_INPUT_TIMER(aTimeoutValue) \
  280. if (!g_InputTimerExists)\
  281.     g_InputTimerExists = SetTimer(g_hWnd, TIMER_ID_INPUT, aTimeoutValue, InputTimeout);
  282.  
  283. // For this one, SetTimer() is called unconditionally because our caller wants the timer reset
  284. // (as though it were killed and recreated) uncondtionally.  MSDN's comments are a little vague
  285. // about this, but testing shows that calling SetTimer() against an existing timer does completely
  286. // reset it as though it were killed and recreated.  Note also that g_hWnd is used vs. NULL so that
  287. // the timer will fire even when a msg pump other than our own is running, such as that of a MsgBox.
  288. #define LARGE_DEREF_BUF_SIZE (4*1024*1024)
  289.  
  290. #define KILL_MAIN_TIMER \
  291. if (g_MainTimerExists && KillTimer(g_hWnd, TIMER_ID_MAIN))\
  292.     g_MainTimerExists = false;
  293.  
  294. // Although the caller doesn't always need g.AllowThreadToBeInterrupted reset to true,
  295. // it's much more maintainable and nicer to do it unconditionally due to the complexity of
  296. // managing quasi-threads.  At the very least, it's needed for when the "idle thread"
  297. // is "resumed" (see MsgSleep for explanation).
  298. #define MAKE_THREAD_INTERRUPTIBLE \
  299. {\
  300.     g.AllowThreadToBeInterrupted = true;\
  301.     KILL_UNINTERRUPTIBLE_TIMER \
  302. }
  303.  
  304. // See above comment about g.AllowThreadToBeInterrupted.
  305. // Also, must restore to true in this case since auto-exec section isn't run as a new thread
  306. // (i.e. there's nothing to resume).
  307. #define KILL_AUTOEXEC_TIMER \
  308. {\
  309.     g.AllowThreadToBeInterrupted = !g.ThreadIsCritical;\
  310.     if (g_AutoExecTimerExists && KillTimer(g_hWnd, TIMER_ID_AUTOEXEC))\
  311.         g_AutoExecTimerExists = false;\
  312. }
  313.  
  314. #define KILL_INPUT_TIMER \
  315. if (g_InputTimerExists && KillTimer(g_hWnd, TIMER_ID_INPUT))\
  316.     g_InputTimerExists = false;
  317.  
  318. #define KILL_DEREF_TIMER \
  319. if (g_DerefTimerExists && KillTimer(g_hWnd, TIMER_ID_DEREF))\
  320.     g_DerefTimerExists = false;
  321.  
  322. #endif
  323.